package org.example.mq.brokerserver.core;

import java.io.Serializable;
import java.util.Arrays;
import java.util.UUID;


public class Message implements Serializable {
    private BasicProperties basicProperties = new BasicProperties();
    private byte[] payload;
    //扩展属性
    private transient long offsetBen;
    private transient long offsetEnd;//添加transient防止被序列化,不需要单独存储
    private byte isValid = 0X1;//和RabbitMQ保存一致
    public String getRoutingKey() {
        return basicProperties.getRoutingKey();
    }

    public void setRoutingKey(String routingKey) {
       basicProperties.setRoutingKey(routingKey);
    }

    public String getUuid() {
        return basicProperties.getUuid();
    }

    public void setUuid(String uuid) {
        basicProperties.setUuid(uuid);
    }
    public BasicProperties getBasicProperties() {
        return basicProperties;
    }

    public void setBasicProperties(BasicProperties basicProperties) {
        this.basicProperties = basicProperties;
    }

    public byte[] getPayload() {
        return payload;
    }

    public void setPayload(byte[] payload) {
        this.payload = payload;
    }

    public long getOffsetBen() {
        return offsetBen;
    }

    public void setOffsetBen(long offsetBen) {
        this.offsetBen = offsetBen;
    }

    public long getOffsetEnd() {
        return offsetEnd;
    }

    public void setOffsetEnd(long offsetEnd) {
        this.offsetEnd = offsetEnd;
    }

    public byte getIsValid() {
        return isValid;
    }

    public void setIsValid(byte isValid) {
        this.isValid = isValid;
    }
    //使用工厂方法来创建
    public static Message createMessageWithId(BasicProperties basicProperties, byte[] payload,String routingKey) {
        Message message = new Message();
        if(basicProperties!=null)
            message.basicProperties = basicProperties;
        message.payload = payload;
        message.setUuid("M-" + UUID.randomUUID());
        message.setRoutingKey(routingKey);
        return message;
    }

    @Override
    public String toString() {
        return "Message{" +
                "basicProperties=" + basicProperties +
                ", payload=" + Arrays.toString(payload) +
                ", offsetBen=" + offsetBen +
                ", offsetEnd=" + offsetEnd +
                ", isValid=" + isValid +
                '}';
    }
}
